/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.survey.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xml.survey.object.AnswerObj;
import xml.survey.object.QuestionObj;
import xml.survey.object.SurveyObj;
import xml.survey.utils.DBUtils;

/**
 *
 * @author CHIP
 */
public class SurveyController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int surveyID = DBUtils.countSurvey();
            String surveyTitle = request.getParameter("txtSurveyTitle");
            String surveyDescription = request.getParameter("txtSurveyDescription");
            SurveyObj survey = new SurveyObj(surveyID, surveyTitle, surveyTitle, surveyDescription, "link");
            boolean rsSurvey = DBUtils.insertSurvey(survey);



            String numberQuestion = request.getParameter("numberQuestion");
            int nQ = Integer.parseInt(numberQuestion);
            String numberAnswer = request.getParameter("numberAnswer");
            int nA = Integer.parseInt(numberAnswer);
            String[] arrQuestion = new String[nQ];
            String[] arrAnswer = new String[nA];
            for (int i = 0; i < nQ; i++) {
                arrQuestion[i] = request.getParameter("txtQuestion" + (i + 1));
            }
            for (int i = 0; i < arrQuestion.length; i++) {
                QuestionObj question = new QuestionObj("answer" +(i+1), arrQuestion[i], surveyID);
                boolean rsQuestion = DBUtils.insertquestion(question);
            }
            
            for (int i = 0; i < nA; i++) {
                for (int j = 0; j < nQ; j++) {
                    if (null != request.getParameter("answer" + (j + 1) + "txtName" + (i + 1))) {
                        QuestionObj ques = new QuestionObj("answer" + (j + 1), arrQuestion[j], surveyID);
                        int questionID = DBUtils.getQuestionID(ques);
                        String answerDescription = request.getParameter("answer" + (j + 1) + "txtName" + (i + 1));
                        AnswerObj answer = new AnswerObj(answerDescription, questionID, surveyID);
                        boolean rsAnswer = DBUtils.insertAnswer(answer);
                        //arrAnswer[i] = request.getParameter("answer" + (j + 1) + "txtName" + (i + 1));
                    }
                }
            }
//            System.out.println("Answer: ");
//            for (int i = 0; i < arrAnswer.length; i++) {
//                AnswerObj answer = new AnswerObj(arrAnswer[i], questionID, surveyID);
//            }
            
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
